#include "functionmainwindow.h"
#include "ui_functionmainwindow.h"
#include "albumwindow.h"
#include "usermessagemainwindow.h"
#include "weatherwindow.h"
#include "camerawindow.h"
#include "voicewindow.h"
#include "ledwindow.h"
#include "beepwindow.h"

FunctionMainWindow::FunctionMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FunctionMainWindow)
{
    ui->setupUi(this);
    show_back_gif();
}

FunctionMainWindow::~FunctionMainWindow()
{
    delete ui;
}

//enum Win{
//    Album_win = 1,User_Message_win,Weather_win,Camera_win,Led_win,Beep_win
//};

//相应语音界面发来的切换界面的信号
void FunctionMainWindow::Voice_window(int num,VoiceWindow *win)
{
    switch (num) {
        case Album_win:
            ui->Album_Btn->clicked();
        break;
        case User_Message_win:
            ui->User_Message_Btn->clicked();
        break;
        case Weather_win:
            ui->Weather_Btn->clicked();
        break;
        case Camera_win:
            ui->Camera_Btn->clicked();
        break;
        case Led_win:
            ui->Led_Btn->clicked();
            ledSignal(1);
        break;
        case Beep_win:
            ui->Beep_Btn->clicked();
        break;
    }
    //    win->close();
}

//显示动态背景图
void FunctionMainWindow::show_back_gif()
{
    QMovie *movepic = new QMovie(QString::fromLocal8Bit(":/new/prefix1/back.gif"));
    movepic->setScaledSize(ui->backGif->size());
    ui->backGif->setMovie(movepic);
    movepic->start();
}

//相册--点击切换界面
void FunctionMainWindow::on_Album_Btn_clicked()
{
    //1.创建界面对象
    AlbumWindow *albumwindow = new AlbumWindow(this);
    albumwindow->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象
    //2.显示要跳转的界面
    albumwindow->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//用户信息--点击切换界面
void FunctionMainWindow::on_User_Message_Btn_clicked()
{
    //1.创建界面对象
    UserMessageMainWindow *user_message_window = new UserMessageMainWindow(this);
    user_message_window->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象
    //2.显示要跳转的界面
    user_message_window->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//天气--点击切换界面
void FunctionMainWindow::on_Weather_Btn_clicked()
{
    WeatherWindow *weather = new WeatherWindow(this);
    weather->setAttribute(Qt::WA_DeleteOnClose);
    //用于跳转天气界面时显示天气
    connect(this,&FunctionMainWindow::weatherSignal,weather,&WeatherWindow::set_city);
    //发送当前城市给天气界面
    weatherSignal(ui->citylabel->text());
    //用于天气界面跳转回功能界面时显示修改后的城市天气
    connect(weather,&WeatherWindow::funSignal,this,&FunctionMainWindow::fun_win);
    weather->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//摄像头--点击切换界面
void FunctionMainWindow::on_Camera_Btn_clicked()
{
    //1.创建界面对象
    CameraWindow *camera_window = new CameraWindow(this);
    camera_window->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象
    //2.显示要跳转的界面
    camera_window->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//语音--点击切换界面
void FunctionMainWindow::on_Voice_Btn_clicked()
{
    //1.创建界面对象--语音界面zhi创建一次
    static VoiceWindow *voice_window = new VoiceWindow(this);
    voice_window->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象
    //连接功能界面与语音界面
    connect(voice_window,&VoiceWindow::voiveSignal,this,&FunctionMainWindow::Voice_window);
    //2.显示要跳转的界面
    voice_window->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//灯--点击切换界面
void FunctionMainWindow::on_Led_Btn_clicked()
{
    //1.创建界面对象--只创建一次
    static  LedWindow *led_window = new LedWindow(this);
    led_window->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象

    connect(this,&FunctionMainWindow::ledSignal,led_window,&LedWindow::led_win);
    //2.显示要跳转的界面
    led_window->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//报警器--点击切换界面
void FunctionMainWindow::on_Beep_Btn_clicked()
{
    //1.创建界面对象
    BeepWindow *beep_window = new BeepWindow(this);
    beep_window->setAttribute(Qt::WA_DeleteOnClose);//关闭后自动销毁对象
    //2.显示要跳转的界面
    beep_window->show();
    //3.跳转到另一个界面，然后隐藏当前界面/关闭当前界面（不能返回原来的界面）
    this->hide();
}

//设置功能界面--当前界面的天气和时间
void FunctionMainWindow::fun_win(QString name)
{
    //获取天气并开启时间定时器
    static QTimer* Date_Time_timer = new QTimer;
    connect(Date_Time_timer,&QTimer::timeout,this,&FunctionMainWindow::show_time);
    Date_Time_timer->start(1000);
    // 1. 准备请求参数
    QNetworkAccessManager *m_manager = new QNetworkAccessManager(this);
    connect(m_manager, &QNetworkAccessManager::finished, this, &FunctionMainWindow::on_weather_reply);
//    QString cityName = "广州"; // 假设有一个城市输入框
    QString url = QString("http://api.seniverse.com/v3/weather/now.json?"
                                 "key=SfgFskzCppFmWqRq1&"
                                 "location=%1&"
                                 "language=zh-Hans&"
                                 "unit=c").arg(name);

    m_manager->get(QNetworkRequest(url));
}

//请求天气后的响应函数--解析Json数据，获取天气信息
void FunctionMainWindow::on_weather_reply(QNetworkReply *reply)
{
    QByteArray data = reply->readAll();
    qDebug()<<QString(data);

    //获取Json数据
    QJsonDocument doc = QJsonDocument::fromJson(data);

    QJsonObject rootObj = doc.object();

    // 解析results数组
    QJsonArray resultsArray = rootObj.value("results").toArray();
    if(resultsArray.isEmpty()) {
        qDebug()<<"未获取到天气数据";
    }

    // 获取第一个结果对象
    QJsonObject resultObj = resultsArray.first().toObject();

    // 获取location和now对象
    QJsonObject locationObj = resultObj.value("location").toObject();
    QJsonObject nowObj = resultObj.value("now").toObject();

    // 提取具体数据
    QString city = locationObj.value("name").toString();
    QString weather = nowObj.value("text").toString();
    QString temperature = nowObj.value("temperature").toString();
    QString code = nowObj.value("code").toString();

    ui->citylabel->setText(city);
    ui->weatherlabel->setText(weather);
    ui->Tlabel->setText(temperature+"°C");

    set_weathercode(code.toUInt());
    qDebug()<<"城市:"<<city;
    qDebug()<<"天气:"<<weather;
    qDebug()<<"温度:"<<temperature<<"°C";
    qDebug()<<"气象代码:"<<code;
}

//显示天气气象代码图片
void FunctionMainWindow::set_weathercode(int code)
{
    switch (code) {
    case 0:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (1).png);");
        break;
    case 1:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (2).png);");
        break;
    case 2:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (3).png);");
        break;
    case 3:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (4).png);");
        break;
    case 4:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (5).png);");
        break;
    case 5:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (6).png);");
        break;
    case 6:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (7).png);");
        break;
    case 7:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (8).png);");
        break;
    case 8:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (9).png);");
        break;
    case 9:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (10).png);");
        break;
    case 10:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (11).png);");
        break;
    case 11:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (12).png);");
        break;
    case 12:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (13).png);");
        break;
    case 13:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (14).png);");
        break;
    case 14:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (15).png);");
        break;
    case 15:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (16).png);");
        break;
    case 16:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (17).png);");
        break;
    case 17:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (18).png);");
        break;
    case 18:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (19).png);");
        break;
    case 19:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (20).png);");
        break;
    case 20:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (21).png);");
        break;
    case 21:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (22).png);");
        break;
    case 22:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (23).png);");
        break;
    case 23:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (24).png);");
        break;
    case 24:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (25).png);");
        break;
    case 25:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (26).png);");
        break;
    case 26:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (27).png);");
        break;
    case 27:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (28).png);");
        break;
    case 28:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (29).png);");
        break;
    case 29:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (30).png);");
        break;
    case 30:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (31).png);");
        break;
    case 31:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (32).png);");
        break;
    case 32:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (33).png);");
        break;
    case 33:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (34).png);");
        break;
    case 34:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (35).png);");
        break;
    case 35:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (36).png);");
        break;
    case 36:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (37).png);");
        break;
    case 37:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (38).png);");
        break;
    case 38:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (39).png);");
        break;
    case 99:ui->codelabel->setStyleSheet("border-image: url(:/new/prefix1/codeblack/02x (40).png);");
        break;
    }
}

//显示实时时间
void FunctionMainWindow::show_time()
{
    //显示实时时间
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}

//关机重启
void FunctionMainWindow::on_close_Btn_clicked()
{
    system("killall -9 ZhijiaVoiceAssistant \n reboot");
}
